{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6d96e659-f743-40cb-bb49-d916a215aa4f",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Real-World Case Studies - FX Options"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bfc0241-b312-45cd-b1b6-cfecab9ad85e",
   "metadata": {},
   "source": [
    "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d0d0add-ff03-4c37-af3c-3942c8adc582",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Load the Libs we need"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4b0d19b3-7892-470c-8051-b3460605ed1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import Lib\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "import pytz\n",
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import yfinance as yf\n",
    "import scipy.stats as si\n",
    "import math\n",
    "import networkx as nx\n",
    "\n",
    "# import module\n",
    "from datetime import datetime, timezone\n",
    "from datetime import date, time\n",
    "from math import trunc\n",
    "from dateutil.parser import parse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36335255-3a12-4108-b282-d524a5e889cb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a3ac8aa6-a494-406c-b86f-e3227c0c28c4",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Introduction to Real-World FX Options Case Studies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cd82d377-850f-44c6-810a-ca74c3283cab",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The price of the European Call Option is: 0.0381\n",
      "The price of the European Put Option is: 0.0639\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy.stats as si\n",
    "\n",
    "def euro_option_pricing(S, K, T, rd, rf, vol, option_type=\"call\"):\n",
    "    # S: Current FX rate\n",
    "    # K: Option strike price\n",
    "    # T: Time to expiration\n",
    "    # rd: domestic interest rate\n",
    "    # rf: foreign interest rate\n",
    "    # vol: Volatility\n",
    "    d1 = (np.log(S / K) + (rd - rf + 0.5 * vol ** 2) * T) / (vol * np.sqrt(T))\n",
    "    d2 = d1 - vol * np.sqrt(T)\n",
    "    if option_type == \"call\":\n",
    "        option_price = (S * np.exp(-rf * T) * si.norm.cdf(d1, 0.0, 1.0) - K * np.exp(-rd * T) * si.norm.cdf(d2, 0.0, 1.0))\n",
    "    elif option_type == \"put\":\n",
    "        option_price = (K * np.exp(-rd * T) * si.norm.cdf(-d2, 0.0, 1.0) - S * np.exp(-rf * T) * si.norm.cdf(-d1, 0.0, 1.0))\n",
    "    return option_price\n",
    "\n",
    "# Example usage\n",
    "S = 1.18  # Current FX rate EURUSD\n",
    "K = 1.20  # Strike Price\n",
    "T = 0.5  # Time to expiration in years\n",
    "rd = 0.01  # Domestic interest rate\n",
    "rf = 0.02  # Foreign interest rate\n",
    "vol = 0.15  # Volatility\n",
    "\n",
    "call_price = euro_option_pricing(S, K, T, rd, rf, vol, option_type=\"call\")\n",
    "put_price = euro_option_pricing(S, K, T, rd, rf, vol, option_type=\"put\")\n",
    "\n",
    "print(f\"The price of the European Call Option is: {call_price:.4f}\")\n",
    "print(f\"The price of the European Put Option is: {put_price:.4f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "669a7722-961e-4095-bc90-2affc5c134e0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "bbbd8cbb-8223-41bd-87fc-5a41170fef43",
   "metadata": {},
   "source": [
    "## FX Options Hedging Strategies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "49e2d97f-ba68-4d2f-8584-75a2ca61b784",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The delta of the European Call Option is: 0.4350\n"
     ]
    }
   ],
   "source": [
    "def delta(S, K, T, rd, rf, vol, option_type=\"call\"):\n",
    "    d1 = (np.log(S / K) + (rd - rf + 0.5 * vol ** 2) * T) / (vol * np.sqrt(T))\n",
    "    if option_type == \"call\":\n",
    "        return np.exp(-rf * T) * si.norm.cdf(d1, 0.0, 1.0)\n",
    "    elif option_type == \"put\":\n",
    "        return -np.exp(-rf * T) * si.norm.cdf(-d1, 0.0, 1.0)\n",
    "\n",
    "# Example usage\n",
    "delta_call = delta(S, K, T, rd, rf, vol, option_type=\"call\")\n",
    "\n",
    "print(f\"The delta of the European Call Option is: {delta_call:.4f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e17d2bf2-aecd-4ac6-9123-1d91821c9bc8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ddf80c7b-f21f-49ff-b730-9e430f5bff6f",
   "metadata": {},
   "source": [
    "## Speculative Trading in FX Options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "15ce8d38-ef69-4137-9332-98365f535405",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The profit from the long call position is: 0.0199\n"
     ]
    }
   ],
   "source": [
    "# Assuming the EURUSD rate goes up to 1.22\n",
    "S_new = 1.22\n",
    "\n",
    "# New call price\n",
    "new_call_price = euro_option_pricing(S_new, K, T, rd, rf, vol, option_type=\"call\")\n",
    "\n",
    "# Profit from the long call position\n",
    "profit = new_call_price - call_price\n",
    "\n",
    "print(f\"The profit from the long call position is: {profit:.4f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "518866a3-5790-4e48-aadf-238b68d7d479",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "29c67e50-7a51-49b9-8385-f8fd2d5d4891",
   "metadata": {},
   "source": [
    "## Risk Management in FX Options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ae0fcb44-0610-43ad-868a-909f83dfb0a3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Value at Risk (VaR) of the options portfolio is: 17.4539\n"
     ]
    }
   ],
   "source": [
    "# Assume a portfolio of 1000 European Call Options\n",
    "portfolio_size = 1000\n",
    "portfolio_value = portfolio_size * call_price\n",
    "\n",
    "# Assume a confidence level of 99%\n",
    "alpha = 0.99\n",
    "VaR = portfolio_value * (1 - si.norm.cdf(vol * np.sqrt(T), 0.0, 1.0))\n",
    "\n",
    "print(f\"The Value at Risk (VaR) of the options portfolio is: {VaR:.4f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d0de498-9d02-4cf0-8204-1f838e6973ae",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ecf1ffce-a612-4050-b4d7-da08c5bdd243",
   "metadata": {},
   "source": [
    "## Implementing Pricing and Valuation Models in FX Options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "04944328-dfc2-4488-a6f6-1f0acd560e40",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The price of the European Call Option with recalibrated volatility is: 0.0479\n"
     ]
    }
   ],
   "source": [
    "# Assume a new recalibrated volatility\n",
    "new_vol = 0.18\n",
    "\n",
    "# New call price with recalibrated volatility\n",
    "new_call_price_recalibrated = euro_option_pricing(S, K, T, rd, rf, new_vol, option_type=\"call\")\n",
    "\n",
    "print(f\"The price of the European Call Option with recalibrated volatility is: {new_call_price_recalibrated:.4f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "156c7f93-6503-49d2-ba32-b2fc74580114",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
